<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>简易计算器</title>

    <style>
        .buttons button {
            position: relative;
            left: 8%;
        }
        
        div img,
        div h1 {
            display: inline-block;
        }
    </style>
</head>

<body>
    <div>
        <img src="images/shop.gif" alt="">
        <h1>简易计算器</h1>
    </div>
    <p>第一个数：<input id="FirstNumber" type="number"></p>
    <p>第二个数：<input id="SecondNumber" type="number"></p>
    <div class="buttons">
        <button onclick="add()">+</button>
        <button onclick="subtract()">-</button>
        <button onclick="multiply()">x</button>
        <button onclick="divide()">÷</button>
    </div>
    <p>计算结果：<input id="result" type="text"></p>

    <script>
        var firstInput = document.getElementById("FirstNumber");
        var secondInput = document.getElementById("SecondNumber");
        var result, resultInput;
        // JavaScript中浮点数会自动省去小数点后无效数字0

        function add() {
            result = parseFloat(firstInput.value) + parseFloat(secondInput.value);
            resultInput = document.getElementById("result");
            resultInput.value = result;
        }

        function subtract() {
            result = parseFloat(firstInput.value) - parseFloat(secondInput.value);
            resultInput = document.getElementById("result");
            resultInput.value = result;
        }

        function multiply() {
            result = parseFloat(firstInput.value) * parseFloat(secondInput.value);
            resultInput = document.getElementById("result");
            resultInput.value = result;
        }

        function divide() {
            result = parseFloat(firstInput.value) / parseFloat(secondInput.value);
            resultInput = document.getElementById("result");
            resultInput.value = result;
        }
    </script>
</body>

</html>